package com.jiejie.util;


import com.jiejie.service.OrderRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
@ServerEndpoint(value = "/webSocket/{customerId}")
public class WebSocketProcess {
    @Resource
    private OrderRedisService orderRedisService;

    /*
     * 持有每个webSocket对象，以key-value存储到线程安全ConcurrentHashMap，
     */
    private static final ConcurrentHashMap<Integer, WebSocketProcess> concurrentHashMap = new ConcurrentHashMap<>(12);

    /**
     * 会话对象
     **/
    private Session session;

    /*
     * 客户端创建连接时触发
     * */
    @OnOpen
    public void onOpen(Session session, @PathParam("customerId") Integer customerId) {
        //每新建立一个连接，就把当前客户id为key,this为value存储到map中
        this.session = session;
        concurrentHashMap.put(customerId, this);
        log.info("Open a websocket. customerId={}", customerId);
    }

    /**
     * 客户端连接关闭时触发
     **/
    @OnClose
    public void onClose(Session session, @PathParam("customerId") Integer customerId) {
        //客户端连接关闭时，移除map中存储的键值对
        concurrentHashMap.remove(customerId);
        log.info("close a websocket, concurrentHashMap remove sessionId= {}", customerId);
    }

    /**
     * 接收到客户端消息时触发
     */
    @OnMessage
    public void onMessage(String message, @PathParam("customerId") Integer customerId) {
        log.info("receive a message from client customerId={},msg={}", customerId, message);
    }

    /**
     * 连接发生异常时候触发
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("Error while websocket. ", error);
    }

    /**
     * 发送消息到指定客户端
     *
     * @param customerId
     * @param message
     */
    public void sendUnpayOrderMessage(Integer customerId, Integer orderId, String message) throws Exception {
        //根据id,从map中获取存储的webSocket对象
        WebSocketProcess webSocketProcess = concurrentHashMap.get(customerId);
        if (!ObjectUtils.isEmpty(webSocketProcess)) {
            //当客户端是Open状态时，才能发送消息
            if (webSocketProcess.session.isOpen()) {
                webSocketProcess.session.getBasicRemote().sendText(message);

            } else {
                orderRedisService.saveUnPayedMessage2Redis(customerId,orderId);
                log.info("websocket session={} is closed ", customerId);

            }
        } else {
            orderRedisService.saveUnPayedMessage2Redis(customerId,orderId);
            log.error("websocket session={} is not exit ", customerId);
        }
    }
    public void sendMessage(Integer customerId, String message) throws Exception {
        //根据id,从map中获取存储的webSocket对象
        WebSocketProcess webSocketProcess = concurrentHashMap.get(customerId);
        if (!ObjectUtils.isEmpty(webSocketProcess)) {
            //当客户端是Open状态时，才能发送消息
            if (webSocketProcess.session.isOpen()) {
                webSocketProcess.session.getBasicRemote().sendText(message);

            } else {
                log.error("websocket session={} is closed ", customerId);

            }
        } else {
            log.error("websocket session={} is not exit ", customerId);
        }
    }

    /**
     * 发送消息到所有客户端
     */
    /*public void sendAllMessage(String msg) throws Exception {
        log.info("online client count={}", concurrentHashMap.size());
        Set<Map.Entry<Integer, WebSocketProcess>> entries = concurrentHashMap.entrySet();
        for (Map.Entry<Integer,WebSocketProcess> entry : entries) {
            Integer cid = entry.getKey();
            WebSocketProcess webSocketProcess = entry.getValue();
            boolean sessionOpen = webSocketProcess.session.isOpen();
            if (sessionOpen) {
                webSocketProcess.session.getBasicRemote().sendText(msg);
            } else {
                log.info("cid={} is closed,ignore send text", cid);
            }
        }
    }*/
}

